package springMvc;

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/tc")
public class TestController extends BaseController{



    @RequestMapping("/test01")
    public void test01(){
        System.out.println("测试访问");
    }

    //只做页面跳转，没有数据传递
    @RequestMapping("/test02")
    public String test02(){
        return "/index.jsp";
    }

    @RequestMapping("/test03")
    @ResponseBody
    public String test03(){
        return "really return String";
    }

    //数据传递，页面跳转
    @RequestMapping("/test04")
    public String test04(HttpServletRequest req){
        User user = new User("hqj123","2356");
        String str = "值传递到跳转的页面";
        Map<String,String> map = new HashMap<String, String>();
        map.put("name","黄芪甲苷");
        map.put("sex","男");
        req.setAttribute("str",str);//第一个str为属性名称，第二个str为属性值
        req.setAttribute("m",map);//传出map集合
        req.setAttribute("u",user);//传出对象

        User user1 = new User("hqj1","2356");
        User user2 = new User("hqj2","2356");
        User user3 = new User("hqj3","2356");
        User user4 = new User("hqj4","2356");
        User user5 = new User("hqj5","2356");
        List<User> list = new ArrayList<User>();
        list.add(user1);list.add(user2);list.add(user3);list.add(user4);list.add(user5);

        req.setAttribute("ls",list);//传出list集合
        return prefix+"/tesst04"+suffix;
    }

    //当只做数据的传递，没有页面跳转
    @RequestMapping("/getUser")
    @ResponseBody
    public String getUser(){
        User u = new User("登录名loginName","密码password");
        //想办法把u 转换为json字符串
        return JSON.toJSONString(u);
    }

    @RequestMapping("/findUser")
    @ResponseBody
    public String findUser(){
        User user1 = new User("hqj1","2356");
        User user2 = new User("hqj2","2356");
        User user3 = new User("hqj3","2356");
        User user4 = new User("hqj4","2356");
        User user5 = new User("hqj5","2356");
        List<User> list = new ArrayList<User>();
        list.add(user1);list.add(user2);list.add(user3);list.add(user4);list.add(user5);
        //想办法把u 转换为json字符串
        return JSON.toJSONString(list);
    }

    @RequestMapping("/getUser02")
    @ResponseBody
    public User getUser02(){
        User u = new User("登录名loginName","密码password");
        return u;
    }
    @RequestMapping("/findUser02")
    @ResponseBody
    public List<User> findUser02(){
        User user1 = new User("hqj1","2356");
        User user2 = new User("hqj2","2356");
        User user3 = new User("hqj3","2356");
        User user4 = new User("hqj4","2356");
        User user5 = new User("hqj5","2356");
        List<User> list = new ArrayList<User>();
        list.add(user1);list.add(user2);list.add(user3);list.add(user4);list.add(user5);
        //想办法把u 转换为json字符串
        return list;
    }

}
